﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP.Classes;
using ERP.Reports;

namespace ERP.QuanlyKho
{
    public partial class viewERP_Nhapkho : DevExpress.XtraEditors.XtraForm
    {
        public cs_erp_hoadonInfo hoadon = new cs_erp_hoadonInfo();
        public cs_hmr_usersInfo user = new cs_hmr_usersInfo();
        public cs_erp_hanghoaInfo hanghoa = new cs_erp_hanghoaInfo();
        public cs_erp_khohangInfo kho = new cs_erp_khohangInfo();
        public cs_erp_nhapkhoInfo nk = new cs_erp_nhapkhoInfo();
        int count = 0;
        public int flag = 1;

        public viewERP_Nhapkho()
        {
            InitializeComponent();
        }

        private void viewERP_Nhapkho_Load(object sender, EventArgs e)
        {
            this.Load_Combo();
            
            if (this.flag == 2 || this.flag == 3)
            {
                this.Load_Info();
                if (this.flag == 3)
                {
                    this.btnLuu.Enabled = false;
                }
            }
            else
            {
                this.txtSoHD.Text = DateTime.Now.Year + "" + DateTime.Now.Month + "" + DateTime.Now.Day + "" + DateTime.Now.Hour + "" + DateTime.Now.Minute  + "" + DateTime.Now.Second + "[" + this.user.username + "]";
            }
            this.Load_Data();
            this.CheckUser();
        }
        private void CheckUser()
        {
            cs_hmr_userrightInfo userright = new cs_hmr_userrightInfo();
            userright.groupid = this.user.groupid;
            cs_hmr_winformInfo wf = new cs_hmr_winformInfo();
            wf.tenform = this.Name;
            if (wf.Get_Info() > 0)
            {
                userright.form_id = wf.formid;
                userright.Get_Info();
                if (userright.savedata == 0)
                {
                    this.btnLuu.Enabled = false;
                }
                if (userright.deletedata == 0)
                {
                }
                if (userright.updatedata == 0)
                {
                    this.btnLuu.Enabled = false;
                }
                if (userright.printreport == 0)
                {
                }
            }
        }
        private void Load_Data()
        {
            DataSet ds = new DataSet("ds");
            ds = this.hanghoa.Get_List_Nhapkho(this.nk.id_lannhap);
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                this.count = 0;
            }
            else
            {
                this.count = ds.Tables[0].Rows.Count;
                this.grcDanhsach.DataSource = ds.Tables[0];
                this.gridView1.ExpandAllGroups();
            }
        }

        private void Load_Combo()
        {
            DataSet ds = new DataSet("ds");
            //this.hanghoa.Get_List = this.kh.id_kh;
            ds = this.kho.Get_List();
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                //this.count = 0;
            }
            else
            {
                //this.cboKhonhap.Properties.DataSource = ds.Tables[0];
                this.cboKhoxuat.Properties.DataSource = ds.Tables[0];
            }
        }

        private void Save_Info()
        {
            this.nk.id_hd = 0;
            this.nk.makho = Convert.ToInt32(this.cboKhoxuat.EditValue.ToString().Trim());
            this.nk.manx = 1;
            this.nk.ngaynhap = DateTime.Now;
            this.nk.nguoinhap = this.user.id_nv;
            this.nk.noidung = this.txtNoidung.Text;
            this.nk.sohd = this.txtSoHD.Text;
            this.nk.ghichu = this.txtGhichu.Text;
        }

        private void Load_Info()
        {
            this.txtNoidung.Text = this.nk.noidung;
            this.txtSoHD.Text = this.nk.sohd;
            this.txtGhichu.Text = this.nk.ghichu;
            this.cboKhoxuat.EditValue = this.nk.makho;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            this.btnLuu.Enabled = false;
            this.Save_Info();
            this.nk.ExcuteProcedure(this.flag + 1);
            this.nk.Get_Info_1();
            //Thực hiện lưu các chi tiết nhập hàng về kho
            for (int i = 0; i < this.count; i++)
            {
                //if (Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcSoluong).ToString().Trim()) > 0)
                //{
                cs_erp_chitiet_nhapkhoInfo ct = new cs_erp_chitiet_nhapkhoInfo();
                ct.makho = Convert.ToInt32(this.cboKhoxuat.EditValue.ToString().Trim());
                ct.nhan = 1;
                ct.id_lannhap = this.nk.id_lannhap;
                ct.mahh = Convert.ToInt32(this.gridView1.GetRowCellValue(i, grdcID).ToString().Trim());
                ct.soluong = Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcSoluong).ToString().Trim());
                //- Convert.ToDouble(this.gridView1.GetRowCellValue(i, gridColumn1).ToString().Trim());
                ct.dongia = Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcDongia).ToString().Trim());
                ct.thanhtien = ct.soluong * ct.dongia;
                ct.hangdu = false;
                ct.ExcuteProcedure(1);

            }
            XtraMessageBox.Show("Lập chứng từ Nhập kho thành công, chờ xác nhận!", "Thông báo");
            this.inHoadon();
            this.Dispose(true);
        }

        private void inHoadon()
        {
            DataSet ds = new DataSet("ds");
            string ban1 = "Số phiếu: " + this.nk.sohd + ", ngày nhập: " + this.nk.ngaynhap.ToShortDateString();
            cs_erp_chitiet_nhapkhoInfo ct = new cs_erp_chitiet_nhapkhoInfo();
            ct.id_lannhap = this.nk.id_lannhap;
            ds = ct.Get_List();
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                MessageBox.Show("Không có số liệu...", "Thông báo!");
                return;
            }
            double sl = 0;
            double thanhtien = 0;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                sl += Convert.ToDouble(ds.Tables[0].Rows[i]["soluong"].ToString().Trim());
                thanhtien += Convert.ToDouble(ds.Tables[0].Rows[i]["thanhtien"].ToString().Trim());
            }
            cs_hmr_donviInfo dv = new cs_hmr_donviInfo();
            dv.captren = "0";
            dv.Get_Info_by_Parents();
            rptNhapkho rpt = new rptNhapkho();
            rpt.DataSource = ds.Tables[0];
            rpt.ds_BaoCao = ds.Tables[0];
            //rpt.dsHoadon = this.hoadon.Get_HD_Tinhtien().Tables[0];
            rpt.ban = ban1;
            rpt.donvi = dv.tendonvi;
            rpt.diachi = dv.diachi;
            rpt.lblDienthoai.Text = dv.sodt_fax;
            rpt.lblTieude.Text = "PHIẾU NHẬP KHO";
            rpt.lbNoidung.Text = this.nk.noidung;
            rpt.lbGhichu.Text = this.nk.ghichu;
            rpt.lblSL.Text = sl.ToString();
            rpt.lblThanhtien.Text = thanhtien.ToString();
            rpt.In();
            rpt.ShowPreview();
            
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose(true);
        }
    }
}